package repository

import (
	"context"
	"gitee.com/xiao_hange/go-admin-log/log/domain"
	"gitee.com/xiao_hange/go-admin-log/log/repository/dao"
	"gitee.com/xiao_hange/go-admin-pkg/pkg/tool"
)

type LogRepository interface {
	InputLog(ctx context.Context, log domain.Log) error
	SearchLog(ctx context.Context, index string, page, limit int) ([]domain.Log, int, error)
}

type logRepository struct {
	dao dao.LogDAO
}

func NewLogRepository(d dao.LogDAO) LogRepository {
	return &logRepository{
		dao: d,
	}
}

func (l *logRepository) InputLog(ctx context.Context, log domain.Log) error {
	return l.dao.Input(ctx, dao.Log{
		Index:   log.Index,
		Message: log.Message,
	})
}

func (l *logRepository) SearchLog(ctx context.Context, index string, page, limit int) ([]domain.Log, int, error) {
	lists, total, err := l.dao.Search(ctx, index, page, limit)
	if err != nil {
		return nil, 0, err
	}
	result := make([]domain.Log, len(lists))
	for i, list := range lists {
		var dtoData domain.Log
		tool.ConvertDAOToDTO(list, &dtoData)
		result[i] = dtoData
	}
	return result, total, err
}
